package com.brillilab.starter.controller.crm;

import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.crm.kitRelateMethod.vo.PublicMethodConvertDto;
import com.brillilab.domain.crm.kitRelateMethod.vo.PublicMethodInfo;
import com.brillilab.domain.po.global.GlobalKit;
import com.brillilab.domain.po.method.MethodType;
import com.brillilab.domain.vo.common.KeyValue;
import com.brillilab.service.core.global.IGlobalKitService;
import com.brillilab.service.logic.crm.KitRelateMethodLogic;
import com.brillilab.service.logic.kit.GlobalKitLogic;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author wuzhiyu
 * @Title: KitRelateMethodController
 * @ProjectName brill_lab
 * @Description: TODO
 * @date 2019/5/2813:53
 */
@RestController
@RequestMapping("/crm/kitRelateMethod")
public class KitRelateMethodController {

    @Resource
    private KitRelateMethodLogic kitRelateMethodLogic;

    @GetMapping("/reagentTypeList")
    public ResponseVo getReagentTypeList() {
        return ResponseVo.success(kitRelateMethodLogic.getPublicThirdReagentTypeList());
    }

    @Resource
    private IGlobalKitService globalKitService;
    @Resource
    private GlobalKitLogic globalKitLogic;

    @GetMapping("/companyList")
    public ResponseVo getCompanyList(Long reagentTypeId) {
        Assert.notNull(reagentTypeId, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(globalKitLogic.searchCompanyByReagentType(reagentTypeId));
    }


    @GetMapping("/searchKitList")
    public ResponseVo searchKitList(Boolean hasAttach, Boolean hasRelateMethod, Long companyId, String reagentName, Long reagentTypeId,Boolean canConvert, int pageNum, int pageSize) {
        Assert.notNull(companyId, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(reagentTypeId, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        if (pageSize == 0 || pageSize > 50) {
            pageSize = 50;
        }
        PageVo<GlobalKit> list = globalKitService.selectKitList(hasAttach, hasRelateMethod, companyId, reagentName, reagentTypeId, canConvert,pageNum, pageSize);
        return ResponseVo.success(list);
    }

    @GetMapping("/kitWithReagentInfo")
    public ResponseVo kitWithReagentInfo(Long kitId) {
        Assert.notNull(kitId, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.getKitWithReagentInfo(kitId));
    }

    @GetMapping("/methodTypeList")
    public ResponseVo methodTypeList() {
        return ResponseVo.success(kitRelateMethodLogic.getThirdPublicMethodType());
    }
    @GetMapping("/allMethodTypeList")
    public ResponseVo allMethodTypeList() {
        return ResponseVo.success(kitRelateMethodLogic.getAllPublicMethodType());
    }
    @GetMapping("/kitRelateMethodStatistics")
    public ResponseVo kitRelateMethodStatistics() {
        return ResponseVo.success(kitRelateMethodLogic.getKitRelateMethodStatistics());
    }

    @GetMapping("/methodDetailInfo")
    public ResponseVo methodDetailInfo(Long methodId) {
        Assert.notNull(methodId, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.getRelateMethodInfo(methodId));
    }

    @GetMapping("/publicMethodList")
    public ResponseVo publicMethodList(Boolean isOnStock, String name, int pageSize, int pageNum) {
        if (pageSize == 0 || pageSize > 20) {
            pageSize = 20;
        }
        return ResponseVo.success(kitRelateMethodLogic.getPublicMethodList(isOnStock, name, pageSize, pageNum));
    }

    @PostMapping("/convert")
    public ResponseVo convert(@RequestBody PublicMethodConvertDto dto) {
        Assert.notNull(dto, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.isTrue(!StringUtils.isEmpty(dto.getMethodText()), ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.convertMethod(dto));
    }

    @PostMapping("/savePublicMethod")
    public ResponseVo savePublicMethod(@RequestBody PublicMethodInfo method) {
        Assert.notNull(method, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.savePublicMethod(method));
    }

    @PostMapping("/onStock")
    public ResponseVo onStock(@RequestBody KeyValue<Long, Integer> value) {
        Assert.notNull(value, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(value.getKey(), ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(value.getValue(), ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.isTrue(value.getValue() == 0 || value.getValue() == 1, ResultEnum.REQUEST_PARAM_ERROR.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.onStock(value));
    }
    @PostMapping("/previewConvert")
    public ResponseVo previceConvert(@RequestBody PublicMethodConvertDto dto) {
        Assert.notNull(dto, ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.isTrue(!StringUtils.isEmpty(dto.getMethodText()), ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.previewConvert(dto));
    }
    @PostMapping("/addThirdMethodType")
    public ResponseVo addThirdMethodType(@RequestBody MethodType methodType){
        Assert.notNull(methodType,ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(methodType.getSecondId(),ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.addPublicThirdMethodType(methodType));
    }
    @PostMapping("/updateAlias")
    public ResponseVo updateAlias(@RequestBody KeyValue<Long,String> data){
        Assert.notNull(data,ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(data.getKey(),ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.isTrue(!StringUtils.isEmpty(data.getValue()),ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.updateReagentAlias(data));
    }
    @PostMapping("/batchRelateMethod")
    public ResponseVo batchRelateMethod(@RequestBody KeyValue<Long, List<Long>> data){
        Assert.notNull(data,ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.notNull(data.getKey(),ResultEnum.REQUEST_PARAM_LACK.getMessage());
        Assert.isTrue(!CollectionUtils.isEmpty(data.getValue()),ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.batchRelateMethod(data));
    }

    @GetMapping
    public ResponseVo setNoProtocol(@RequestBody Long globalKitId){
        Assert.notNull(globalKitId,ResultEnum.REQUEST_PARAM_LACK.getMessage());
        return ResponseVo.success(kitRelateMethodLogic.setNoProtocol(globalKitId));
    }
}
